package com.xiaozhengkeji.blogsuper.api;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.xiaozhengkeji.blogsuper.base.entitys.ArticleClassEntity;
import com.xiaozhengkeji.blogsuper.base.entitys.UserEntity;
import com.xiaozhengkeji.blogsuper.base.service.ArticleClassEntityService;
import com.xiaozhengkeji.blogsuper.base.service.SystemConfigEntityService;
import com.xiaozhengkeji.blogsuper.base.service.UserEntityService;
import com.xiaozhengkeji.blogsuper.bean.ResponseBean;
import com.xiaozhengkeji.blogsuper.bean.type.ResCodeType;
import com.xiaozhengkeji.blogsuper.utils.ArticleUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

/**
 * 文章分类Api
 */
@Slf4j
@RestController
@RequestMapping("/web/Article/class")
public class ArticleClassApi {
    @Autowired
    UserEntityService userEntityService;
    @Autowired
    ArticleClassEntityService articleClassEntityService;
    @Autowired
    SystemConfigEntityService systemConfigEntityService;

    /**
     * 获取当前用户可查看的全部分类
     */
    @ApiOperation("获取当前用户可查看的全部分类 (部分分类需要指定权限) 可不登录")
    @GetMapping("getClass")
    public ResponseBean getArticleClass() {
        //先看看用户有没有登录
        Object UserID = StpUtil.getLoginIdDefaultNull();
        List<String> UserArticleClassPurview = new LinkedList<>();
        //给与默认权限
        UserArticleClassPurview.add("user:def");
        if (UserID != null) {
            //用户登录了给与数据库权限
            UserEntity userEntity = userEntityService.selectByPrimaryKey(StpUtil.getLoginIdAsInt());
            UserArticleClassPurview.addAll(JSON.parseArray(userEntityService.selectByPrimaryKey(userEntity.getId()).getPermission(), String.class));
        }
        //创建即将返回的数据
        List<ArticleClassEntity> ret_ArticleClass = new LinkedList<>();
        //获取文章分类的全部数据
        for (ArticleClassEntity articleClass : articleClassEntityService.queryAllByEnable(1)) {
            //利用工具进行判断 用户是否可以查看此分类文章
            if (ArticleUtils.isPermissionsTableUseArticleClass(UserArticleClassPurview, articleClass)) {
                //如果有权限则 如果有则添加
                ret_ArticleClass.add(articleClass);
            }
        }
        return new ResponseBean(ResCodeType.Success, "获取完成!", ret_ArticleClass, ret_ArticleClass.size());
    }

    /**
     * 添加一个文章分类
     */
    @ApiOperation("添加一个文章分类 需要权限(admin:all)")
    @PostMapping("addClass")
    @SaCheckPermission("admin:all")
    public ResponseBean addArticleClass(@ApiParam("分类名") @RequestParam("CategoryName") String CategoryName,
                                        @ApiParam("权重") @RequestParam("Weights") Integer Weights,
                                        @ApiParam("自定义分类路径") @RequestParam("CustomPathName") String CustomPathName,
                                        @ApiParam("此分类需要权限 例:[\"user:def\",\"admin:all\"]") @RequestParam("ClassificationAuthority") String ClassificationAuthority) {
        if (articleClassEntityService.queryByCategoryName(CategoryName).size() > 0) {
            return new ResponseBean(ResCodeType.Error, "已经存在此分类!", null, null);
        }
        ArticleClassEntity articleClassEntity = new ArticleClassEntity();
        articleClassEntity.setWeights(Weights);
        articleClassEntity.setCategoryName(CategoryName);
        articleClassEntity.setEnable(1);
        articleClassEntity.setClassificationAuthority(ClassificationAuthority);
        articleClassEntity.setCustomPathName(CustomPathName);
        articleClassEntityService.insert(articleClassEntity);
        return new ResponseBean(ResCodeType.Success, "添加分类完成!!", null, null);
    }

    /**
     * 开关文章分类
     */
    @ApiOperation("开关文章分类 需要权限(admin:all)")
    @GetMapping("enableClass")
    @SaCheckPermission("admin:all")
    public ResponseBean enableClass(@ApiParam("分类名") @RequestParam("CategoryName") String CategoryName,
                                    @ApiParam("是否启用") @RequestParam("Enable") Integer Enable) {
        if (articleClassEntityService.queryByCategoryName(CategoryName).size() < 1) {
            return new ResponseBean(ResCodeType.Error, "未找到此分类!", null, null);
        }
        ArticleClassEntity articleClassEntity = articleClassEntityService.queryByCategoryName(CategoryName).get(0);
        articleClassEntity.setEnable(Enable);
        articleClassEntityService.updateByPrimaryKey(articleClassEntity);
        return new ResponseBean(ResCodeType.Success, "开关分类完成!!", articleClassEntity.getEnable(), null);
    }

    /**
     * 修改文章分类的权限
     */
    @ApiOperation("修改文章分类的权限 需要权限(admin:all)")
    @GetMapping("authorityClass")
    @SaCheckPermission("admin:all")
    public ResponseBean authorityClass(@ApiParam("分类名") @RequestParam("CategoryName") String CategoryName,
                                       @ApiParam("权限 例:[\"user:def\",\"admin:all\"]") @RequestParam("ClassificationAuthority") String ClassificationAuthority) {
        if (articleClassEntityService.queryByCategoryName(CategoryName).size() < 1) {
            return new ResponseBean(ResCodeType.Error, "未找到此分类!", null, null);
        }
        ArticleClassEntity articleClassEntity = articleClassEntityService.queryByCategoryName(CategoryName).get(0);
        articleClassEntity.setClassificationAuthority(ClassificationAuthority);
        articleClassEntityService.updateByPrimaryKey(articleClassEntity);
        return new ResponseBean(ResCodeType.Success, "修改文章分类完成!!", articleClassEntity.getEnable(), null);
    }
}
